package com.artqiyi.dollmachine.views.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.artqiyi.dollmachine.R;
import com.artqiyi.dollmachine.common.Base.BaseActivity;
import com.artqiyi.dollmachine.common.constant.Constant;
import com.artqiyi.dollmachine.presenter.base.BasePresenter;
import com.artqiyi.dollmachine.utils.DensityUtil;
import com.artqiyi.dollmachine.views.adapter.OperateDetailPageAdapter;
import com.artqiyi.dollmachine.views.fragment.OperateCatchRecordFragment;
import com.artqiyi.dollmachine.views.fragment.OperateDetailFragment;
import com.artqiyi.dollmachine.views.widget.ToolBarView;

import java.lang.reflect.Field;

import butterknife.Bind;
import timber.log.Timber;

/**
 * author: PengCheng
 * time: 2017/12/30 0030
 * desc: 操作详情窗口
 */

public class OperateDetailDialogActivity extends BaseActivity implements View.OnClickListener{
    private int mRequestCode = 100;
    @Bind(R.id.tabLayout)
    TabLayout mTabLayout;
    @Bind(R.id.imageClose)
    ImageView imageClose;

    private OperateCatchRecordFragment mOperateCatchRecordFragment;

    private OperateDetailFragment mOperateDetailFragment;

    @Bind(R.id.viewPager)
    ViewPager mViewPager;

    private int machineId;
    private String machineDetailImage;
    @Override
    public BasePresenter getPresenter() {
        return null;
    }

    @Override
    protected void handleIntent(Intent intent) {
        super.handleIntent(intent);
        machineId = getIntent().getIntExtra(Constant.KEY_MACHINE_ID, 0);
        machineDetailImage = getIntent().getStringExtra(Constant.KEY_MACHINE_DETAIL_IMAGE);
    }
    @Override
    public int getLayoutId() {
        return R.layout.dialog_detail;
    }

    @Override
    protected void handleToolBar(ToolBarView toolBar) {
        super.handleToolBar(toolBar);
        toolBar.setVisibility(View.GONE);
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        imageClose.setOnClickListener(this);
        String[] titles = {getString(R.string.operate_detail), getString(R.string.operate_catch_record)};
        mOperateDetailFragment = new OperateDetailFragment();
        Bundle detailBundle = new Bundle();
        detailBundle.putString(Constant.KEY_MACHINE_DETAIL_IMAGE, machineDetailImage);
        mOperateDetailFragment.setArguments(detailBundle);

        mOperateCatchRecordFragment = new OperateCatchRecordFragment();
        Bundle catchRecordBundle = new Bundle();
        catchRecordBundle.putInt(Constant.KEY_MACHINE_ID, machineId);
        mOperateCatchRecordFragment.setArguments(catchRecordBundle);
        Fragment[] fragments = {mOperateDetailFragment, mOperateCatchRecordFragment};
        OperateDetailPageAdapter adapter = new OperateDetailPageAdapter(getSupportFragmentManager(), fragments, titles);
        mViewPager.setAdapter(adapter);
        mViewPager.setOffscreenPageLimit(titles.length);
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        mTabLayout.setupWithViewPager(mViewPager);
    }

    @Override
    public void loadData() {
        super.loadData();
        setTabLayout();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.imageClose:
                onBackPressed();
                break;
        }
    }

    @Override
    public void onBackPressed() {
        setResult(mRequestCode);
        OperateDetailDialogActivity.this.finish();
    }

    //用反射来设置tablayout宽度
    public void setTabLayout(){
        mTabLayout.post(new Runnable() {
            @Override
            public void run() {
                try {
                    //拿到tabLayout的mTabStrip属性
                    Field mTabStripField = mTabLayout.getClass().getDeclaredField("mTabStrip");
                    mTabStripField.setAccessible(true);
                    LinearLayout mTabStrip = (LinearLayout) mTabStripField.get(mTabLayout);
                    int dp10 = DensityUtil.dp2px(10);
//                    int dp10 = DensityUtil.dp2px(tabLayout.getContext(), 10);
                    for (int i = 0; i < mTabStrip.getChildCount(); i++) {
                        View tabView = mTabStrip.getChildAt(i);
                        //拿到tabView的mTextView属性
                        Field mTextViewField = tabView.getClass().getDeclaredField("mTextView");
                        mTextViewField.setAccessible(true);
                        TextView mTextView = (TextView) mTextViewField.get(tabView);
                        tabView.setPadding(0, 0, 0, 0);
                        //因为我想要的效果是   字多宽线就多宽，所以测量mTextView的宽度
                        int width = 0;
                        width = mTextView.getWidth();
                        if (width == 0) {
                            mTextView.measure(0, 0);
                            width = mTextView.getMeasuredWidth();
                        }
                        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tabView.getLayoutParams();
                        params.width = width ;
                        params.leftMargin = dp10;
                        params.rightMargin = dp10;
                        tabView.setLayoutParams(params);
                        tabView.invalidate();
                    }
                    setTabDivider();
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void setTabDivider() {
        LinearLayout linearLayout = (LinearLayout) mTabLayout.getChildAt(0);
        linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        linearLayout.setDividerPadding(DensityUtil.dp2px(5));
        linearLayout.setDividerDrawable(ContextCompat.getDrawable(this,
                R.drawable.divider_vertical));
    }

}
